var express = require("express")
var router = express.Router()
var QcloudSms = require("qcloudsms_js")
var connection = require("../../../db/sql")
var jwt = require("jsonwebtoken")
const bcrypt = require("bcryptjs")

var AlipayFormData = require("alipay-sdk/lib/form").default
// import axios from "axios"
const Sql = require("./sql")
var axios = require("axios")
const { result } = require("lodash")

// 请求数据
router.get("/api/news/getdata", function (req, res, next) {
  connection.query(Sql.selectData(), (err, results) => {
    if (err) {
      res.send({
        code: 201,
        msg: "查询失败",
        err,
      })
    }

    res.send({
      code: 200,
      msg: "查询成功",
      data: results,
    })
  })
})

// 查询详情信息
router.post("/api/news/getdetail", function (req, res, next) {
  let id = req.body.id
  console.log(id)
  connection.query(Sql.selectDetail(id), (err, result) => {
    if (err) {
      res.send({
        code: 201,
        msg: "查询失败",
        err,
      })
    }
    // 获取图片
    connection.query(Sql.selectPic(id), (e, res1) => {
      if (e) {
        res.send({
          code: 201,
          msg: "查询失败",
          e,
        })
      }
      res.send({
        code: 200,
        msg: "查询成功",
        data: result,
        pic: res1,
      })
    })
  })
})

// 插入数据
router.post("/api/news/putdata", function (req, res, next) {
  let obj = req.body
  console.log(obj)
  // 插入数据
  connection.query(Sql.insertData(obj), (err, result) => {
    if (err) {
      res.send({
        code: 201,
        msg: "错误",
        err,
      })
    }
    // 获取插入的id
    const id = result.insertId
    console.log(`Inserted ID: ${id}`)

    const promises = []

    // 定义每次插入的操作
    for (let i = 0; i < obj.imgs.length; i++) {
      const promise = new Promise((resolve, reject) => {
        connection.query(Sql.insertPic(id, obj.imgs[i]), (error, results) => {
          if (error) {
            reject(error)
          } else {
            resolve(results)
          }
        })
      })

      promises.push(promise)
    }

    // 等待所有插入操作完成
    Promise.all(promises)
      .then((results) => {
        // 所有插入操作都成功完成
        console.log("Inserted pics:", results)

        res.send({
          code: 200,
          msg: "成功",
        })
      })
      .catch((error) => {
        // 插入操作失败
        console.error("Failed to insert pics:", error)

        res.send({
          code: 201,
          msg: "错误",
          error,
        })
      })
  })
})

// 撤销

router.post("/api/news/deldata", function (req, res, next) {
  let id = req.body.id
  console.log(id)
  // 删除关联
  connection.query(Sql.delPics(id), (err, result) => {
    if (err) {
      res.send({
        code: 201,
        msg: "删除失败",
        err,
      })
    }

    connection.query(Sql.delNews(id), (e, results) => {
      if (e) {
        res.send({
          code: 202,
          msg: "删除失败",
          err,
        })
      }

      res.send({
        code: 200,
        msg: "删除成功",
      })
    })
  })
})

module.exports = router
